﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class UpdateGuest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int bookingID = Convert.ToInt32(Request.QueryString["BookingID"]);
                BookingInfo b = BookingAndRoomAvailabilityDBManager.GetBookingInfo(bookingID);
                lblBookingId.Text = bookingID.ToString();

                DateTime arrivalDateOnly = b.ArrivalDate.Date;
                ddlArrivalDay.SelectedValue= (arrivalDateOnly.Day).ToString();
                ddlArrivalMonth.SelectedValue = (arrivalDateOnly.Month).ToString();
                tbxArrivalYear.Text = (arrivalDateOnly.Year).ToString();

                tbxArrivalTime.Text = b.ArrivalTime;

                DateTime departDateOnly = b.DepartDate.Date;
                ddlDepartDay.SelectedValue = (departDateOnly.Day).ToString();
                ddlDepartMonth.SelectedValue = (departDateOnly.Month).ToString();
                tbxDepartYear.Text = (departDateOnly.Year).ToString();

                tbxDepartTime.Text = b.DepartTime;

                tbxNumOfNight.Text = b.NumOfNight.ToString();
                tbxNumOfAdult.Text = b.NumOfAdult.ToString();
                tbxNumOfChildren.Text = b.NumOfChildren.ToString();

                tbxRemarks.Text = b.Remarks;
                tbxArea.Text = b.Area;
                rbPaymentOption.Text = b.PaymentMode;

                Room r = BookingAndRoomAvailabilityDBManager.GetRoomInfoByBookingId(bookingID);
                string roomNum=r.RoomNum;
                ArrayList roomIdList = BookingAndRoomAvailabilityDBManager.GetAllRoomID();
                ddlRoomNo.DataSource = roomIdList;
                ddlRoomNo.DataBind();
               
                ddlRoomNo.SelectedValue = roomNum;
                tbxRoomType.Text = r.TypeOfRoom;

                Guest g = BookingAndRoomAvailabilityDBManager.GetGuestInfoByBookingId(bookingID);
                lblGuestId.Text = g.GuestID.ToString();

                ddlTitle.SelectedValue = g.Title;
                tbxFirstName.Text = g.FirstName;
                tbxLastName.Text = g.LastName;
                rbGender.Text = g.Gender;

                DateTime dobDateOnly = g.DOB.Date;
                ddlDobDay.SelectedValue = (dobDateOnly.Day).ToString();
                ddlDobMonth.SelectedValue = (dobDateOnly.Month).ToString();
                tbxDobYear.Text = (dobDateOnly.Year).ToString();

                tbxEmail.Text = g.Email;
                tbxMobileNum.Text = g.MobileNum.ToString();
                tbxHomeNum.Text = g.HomeNum.ToString();
                tbxAddress.Text = g.Address;
                tbxCity.Text = g.City;
                tbxState.Text = g.State;
                tbxZip.Text = g.ZipCode.ToString();
                tbxCountry.Text = g.Country;

                int guestIdforSearch = Convert.ToInt32(lblGuestId.Text);
                CreditCard c = BookingAndRoomAvailabilityDBManager.GetCreditCardInfoByGuestID(guestIdforSearch);
                tbxCardHolderName.Text = c.CardHolderName;
                ddlType.SelectedValue = c.Type;
                lblCardNum.Text = c.CardNum;

                DateTime expiryDateOnly = c.ExpiryDate;
                ddlExpiryDay.SelectedValue = (expiryDateOnly.Day).ToString();
                ddlExpiryMonth.SelectedValue = (expiryDateOnly.Month).ToString();
                tbxExpiryYear.Text = (expiryDateOnly.Year).ToString();
            }
        }

        protected void btnBack2_Click(object sender, EventArgs e)
        {
            Server.Transfer("UpdateCheckInGuest.aspx");
        }

        protected void btnUpdateGuest_Click(object sender, EventArgs e)
        {
            string arrivalDate = ddlArrivalDay.SelectedValue + "/" + ddlArrivalMonth.SelectedValue + "/" + tbxArrivalYear.Text;
            DateTime dt_arrivalDate = DateTime.Parse(arrivalDate);

            string arrivalTime = tbxArrivalTime.Text;

            string departDate = ddlDepartDay.SelectedValue + "/" + ddlDepartMonth.SelectedValue + "/" + tbxDepartYear.Text;
            DateTime dt_departDate = DateTime.Parse(departDate);

            string departTime = tbxDepartTime.Text;

            int numOfNight = Convert.ToInt32(tbxNumOfNight.Text);
            int numOfAdult = Convert.ToInt32(tbxNumOfAdult.Text);
            int numOfChildren = Convert.ToInt32(tbxNumOfChildren.Text);

            string roomNum = ddlRoomNo.SelectedValue;
            string roomType = tbxRoomType.Text;

            string remarks = tbxRemarks.Text;
            string area = tbxArea.Text;
            string paymentOption = rbPaymentOption.SelectedValue;
            int guestId = Convert.ToInt32(lblGuestId.Text);

            int bookingId = Convert.ToInt32(lblBookingId.Text);

            string title=ddlTitle.SelectedValue;
            string firstName=tbxFirstName.Text;
            string lastName=tbxLastName.Text;
            string gender=rbGender.SelectedValue;

            string dob=ddlDobDay.SelectedValue + "/" + ddlDobMonth.SelectedValue + "/" + tbxDobYear.Text;
            DateTime dt_dob=DateTime.Parse(dob);

            string email=tbxEmail.Text;
            int mobileNum=Convert.ToInt32(tbxMobileNum.Text);

            int homeNum=Convert.ToInt32(tbxHomeNum.Text);

            string address=tbxAddress.Text;
            string city=tbxCity.Text;
            string state=tbxState.Text;
            int zipCode=Convert.ToInt32(tbxZip.Text);
            string country=tbxCountry.Text;

            string cardHolderName=tbxCardHolderName.Text;
            string type=ddlType.SelectedValue;
            string cardNum=lblCardNum.Text;
            string expiryDate=ddlExpiryDay.SelectedValue + "/" + ddlExpiryMonth.SelectedValue + "/" +tbxExpiryYear.Text;
            DateTime dt_expiryDate=DateTime.Parse(expiryDate);

            BookingInfo updatedBooking = new BookingInfo();

            updatedBooking.ArrivalDate = dt_arrivalDate;
            updatedBooking.ArrivalTime = arrivalTime;
            updatedBooking.DepartDate = dt_departDate;
            updatedBooking.DepartTime = departTime;
            updatedBooking.NumOfNight = numOfNight;
            updatedBooking.NumOfAdult = numOfAdult;
            updatedBooking.NumOfChildren = numOfChildren;
            updatedBooking.RoomNum = roomNum;
            updatedBooking.PaymentMode = paymentOption;
            updatedBooking.Remarks = remarks;
            updatedBooking.Area = area;
            updatedBooking.GuestID = guestId;

            Guest updatedGuest = new Guest();

            updatedGuest.Title = title;
            updatedGuest.FirstName = firstName;
            updatedGuest.LastName = lastName;
            updatedGuest.Gender = gender;
            updatedGuest.DOB = dt_dob;
            updatedGuest.Email = email;
            updatedGuest.MobileNum = mobileNum;
            updatedGuest.HomeNum = homeNum;
            updatedGuest.Address = address;
            updatedGuest.City = city;
            updatedGuest.State = state;
            updatedGuest.ZipCode = zipCode;
            updatedGuest.Country = country;

            BookingAndRoomAvailabilityDBManager.UpdateGuestInfo(updatedGuest, Convert.ToInt32(lblGuestId.Text));

           
            CreditCard updatedCreditCard = new CreditCard();
            updatedCreditCard.CardHolderName = cardHolderName;
            updatedCreditCard.Type = type;
       
            updatedCreditCard.ExpiryDate = dt_expiryDate;

           
            BookingAndRoomAvailabilityDBManager.UpdateCreditCardInfo(updatedCreditCard, Convert.ToInt32(lblGuestId.Text));

            string querystring = "bookingID=" + lblBookingId.Text;
            querystring += "&" + "guestID=" + lblGuestId.Text;

            if (BookingAndRoomAvailabilityDBManager.UpdateBookingInfo(updatedBooking, Convert.ToInt32(lblBookingId.Text)) == 1)
            {
                Server.Transfer("UpdateSuccess.aspx?"+ querystring);
            }
            else
            {
                lblStatus.Text = "Booking and Guest Information have not been successfully updated";
            }
          
        }
    }
}